package jok.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterDemo {

	public static void main(String[] args) {

		// writeFile();
		writeFile2();

		FileDemo.deleteFile("D:\\a.txt");
	}

	public static void writeFile() {
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter("D:/a.txt"));
			bw.write("HelloWorld");
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bw != null)
					bw.close();// 关闭BufferedWriter会自动关闭FileWriter
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void writeFile2() {
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter("D:/a.txt"));
			for (int i = 0; i < 100; i++) {
				bw.write("HelloWorld");
				bw.newLine();// 换行
				if (i % 10 == 0) {
					bw.flush(); // 每10条flush一次
				}
			}
			bw.flush();// 最后剩余的再flush一次
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bw != null)
					bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
